/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __frmodule_register.c                              */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/03 by geyh                                 */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <dlfcn.h>
#include <sys/prctl.h>

#include "frcomm.h"
#include "frmem.h"
#include "apmtest_cunit.h"
#include "frlist.h"

#include "conntrack.h"
#include "frmodule_mng.h"
#include "frct_comm.h"

#include "frxml_parse.h"
#include "frversion.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

extern char g_arr_frame_version[30];
extern void *g_p_memstat;
extern int g_i_flag_run;

extern Frlist_header *g_p_st_hdr_mng_module_del;
extern Frlist_header *g_p_st_hdr_mng_module_use;
void __list_node_mng_free(Frlist_node *p_st_node);

static int test_my_create(void)
{
    g_p_memstat = fr_slab_init2(5242880, "framework");

    //初始化
    g_p_st_hdr_mng_module_use = frlist_create();
    if(g_p_st_hdr_mng_module_use == NULL)
        return FR_FAIL;

    g_p_st_hdr_mng_module_del = frlist_create();
    if(g_p_st_hdr_mng_module_del == NULL)
    {
        frfree(g_p_st_hdr_mng_module_use);
        return FR_FAIL;
    }

    return FR_SUCC;
}

static void test_my_destroy(void)
{
    if(g_p_st_hdr_mng_module_del)
    {
        frlist_destroy(g_p_st_hdr_mng_module_del, __list_node_mng_free);
        g_p_st_hdr_mng_module_del = NULL;
    }

    if(g_p_st_hdr_mng_module_use)
    {
        frlist_destroy(g_p_st_hdr_mng_module_use, __list_node_mng_free);
        g_p_st_hdr_mng_module_use = NULL;
    }

    fr_slab_destroy();
    return;
}

int __frmodule_register(const char *p_module_filenm
        , const char *p_so_filenm
        , int i_l7_proto
        , const char *p_module_name
        , const char *p_params);


static void case1(void)
{
    const char *p_module_filenm = "module.xml";
    const char *p_so_filenm = "../../../../src/module/syncmodule/syncmodule_3.0.0.so";
    int i_l7_proto = 1;
    const char *p_module_name = "syncmodule";
    void  *p_so_handle = dlopen("../../../../src/module/syncmodule/syncmodule_3.0.0.so", RTLD_LAZY);
    int i_ret = 0;

    //初始化
    test_my_create();
    
    i_ret =  __frmodule_register(p_module_filenm, p_so_filenm, i_l7_proto, p_module_name, "");
    
    CU_ASSERT_EQUAL(i_ret, 0);

    test_my_destroy();
    dlclose(p_so_handle);
    return;

}

static void case2(void)
{
    const char *p_module_filenm = "module.xml";
    const char *p_so_filenm = "../../../../src/module/syncmodule/syncmodule_3.0.0.so";
    int i_l7_proto = 1;
    const char *p_module_name = "syncmodule";
    void  *p_so_handle = dlopen("../../../../src/module/syncmodule/syncmodule_3.0.0.so", RTLD_LAZY);
    int i_ret = 0;

    //初始化
    test_my_create();

    i_ret =  __frmodule_register(p_module_filenm, p_so_filenm, i_l7_proto, p_module_name, "");
    i_ret =  __frmodule_register(p_module_filenm, p_so_filenm, i_l7_proto, p_module_name, "");

    CU_ASSERT_EQUAL(i_ret, 2);

    test_my_destroy();
    dlclose(p_so_handle);
    return;

}

static void case3(void)
{
    const char *p_module_filenm = "module.xml";
    const char *p_so_filenm = NULL;
    int i_l7_proto = 1;
    const char *p_module_name = "syncmodule";
    void  *p_so_handle = dlopen("../../../../src/module/syncmodule/syncmodule_3.0.0.so", RTLD_LAZY);
    int i_ret = 0;

    //初始化
    test_my_create();

    //加载
    i_ret = frmodule_register_path("module.xml");
    if(i_ret != 0)
    {
        frdbg("error\n");
        return;
    }

    //卸载
    i_ret = frmodule_unregister_path("module.xml", 1);
    if(i_ret != 0)
    {
        frdbg("error\n");
        return;
    }

    i_ret =  __frmodule_register(p_module_filenm, p_so_filenm, i_l7_proto, p_module_name, "");

    CU_ASSERT_EQUAL(i_ret, 1);

    test_my_destroy();
    dlclose(p_so_handle);
    return;

}

static void case4(void)
{
    const char *p_module_filenm = "module.xml";
    const char *p_so_filenm = "../../../../src/module/syncmodule/syncmodule_3.0.0.so";
    int i_l7_proto = 1;
    const char *p_module_name = "syncmodule";
    void  *p_so_handle = dlopen("../../../../src/module/syncmodule/syncmodule_3.0.0.so", RTLD_LAZY);
    int i_ret = 0;

    //初始化
    test_my_create();

    //加载
    i_ret = frmodule_register_path("module.xml");
    if(i_ret != 0)
    {
        frdbg("error\n");
        return;
    }

    //卸载
    i_ret = frmodule_unregister_path("module.xml", 1);
    if(i_ret != 0)
    {
        frdbg("error\n");
        return;
    }

    i_ret =  __frmodule_register(p_module_filenm, p_so_filenm, i_l7_proto, p_module_name, "");

    CU_ASSERT_EQUAL(i_ret, 0);

    test_my_destroy();
    dlclose(p_so_handle);
    return;

}

int add_suite()
{
    CU_pSuite pSuite = NULL;

    pSuite = CU_add_suite("__frmodule_register", __suite_init, __suite_cleanup);

    if (NULL == pSuite)
    {
        return 1;
    }

    //增加测试用例
    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);
    CU_add_test(pSuite, "case4", case4);
//    CU_add_test(pSuite, "case5", case5);
//    CU_add_test(pSuite, "case6", case6);
//    CU_add_test(pSuite, "case7", case7);
//    CU_add_test(pSuite, "case8", case8);
//    CU_add_test(pSuite, "case9", case9);
//    CU_add_test(pSuite, "case10", case10);

    return 0;
}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");

    xmlInitParser();
    frversion_set(g_arr_frame_version, sizeof(g_arr_frame_version), 0);

    run_cunit(add_suite);

    xmlCleanupParser();
    fr_slab_destroy();

    return 0;
}


